package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.vRepaymentInfo;
import com.gxa.p2p.service.RepaymentDetailService;
import com.gxa.p2p.util.MapUtil;
import com.gxa.p2p.util.ReturnMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@ResponseBody
@RequestMapping("/RepaymentDetail")
@Api(value = "用户还款控制模块",tags = "负责用户还款的模块")
public class RepaymentDetailController {
    @Resource(name = "repaymentDetailServiceImpl")
    RepaymentDetailService repaymentDetailService;

    @GetMapping("/findInfo")
    @ApiOperation(value = "查询用户还款详细记录接口",tags = "查询用户还款信息接口")
    public Map<String, Object> findInfo(Integer userId,Integer page,Integer limit){
        List<Object> info = repaymentDetailService.findInfo(userId, page, limit);
        ReturnMap<Object> returnMap = new ReturnMap<>();
        return returnMap.returnMap(info);
    }
    @GetMapping("/findInfoDim")
    @ApiOperation(value = "模糊查询用户还款详细记录接口",tags = "模糊查询用户还款信息接口")
    public Map<String, Object> findInfoDim(@RequestParam("userId") Integer userId,
                                           @RequestParam("periodType") String periodType,
                                           @RequestParam("page") Integer page,
                                           @RequestParam("limit") Integer limit)
    {
        List<Object> info = repaymentDetailService.findInfoDim(userId,periodType, page, limit);
        ReturnMap<Object> returnMap = new ReturnMap<>();
        return returnMap.returnMap(info);
    }
    @PutMapping
    @ApiOperation(value = "用户进行还款接口",tags = "用户进行还款操作接口")
    public Map<String, Object> actualUpdate(@RequestBody vRepaymentInfo vRepaymentInfo){
        // 更新还款记录表
        MapUtil mapUtil = new MapUtil();
        if (repaymentDetailService.actualUpdate(vRepaymentInfo))
        {
            mapUtil.setCode(0);
            mapUtil.setMsg("成功");
        }else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        //返回数据
        return mapUtil.getMap();

    }
}
